package com.meizi.utils;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

/**
 * @author Administration
 * @date 2021/8/6 - 13:55 - 星期五
 * @project study
 * @package com.meizi.utils
 * @by GGbomd猪猪侠
 */
public class ExecutorServiceUtilsNew {
    private static ExecutorService es = Executors.newFixedThreadPool(15);

    private static CompletionService<Object> completionService = new ExecutorCompletionService<Object>(es);

    public static Future execute(Consumer<Object> consumer, Object obj) throws Exception {


        Future<Object> future = completionService.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                consumer.accept(obj);
                return obj;
            }
        });
        return future;
    }

    public static Future execute(Consumer<Object> consumer) throws Exception {
        return execute(consumer, null);
    }

    public static void main(String[] args) throws Exception {

        AtomicInteger count = new AtomicInteger();

        for(int i = 0; i < 2000; i++){
            ExecutorServiceUtilsNew.execute(x -> {
                int intx = (int)x;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(x);

                int i1 = count.addAndGet(1);

                if(i1 % 100 == 0) {
                    System.out.println("");
                    System.out.println("");
                    System.out.println("");
                    System.out.println("");
                    System.out.println("");
                }
            }, i);
        }
    }
}
